<?php
namespace app\admin\controller;

use think\Controller;
use \think\Session;
use think\Request;
use app\admin\model\AdminUser;
use think\Db;

class CommonController extends Controller
{
	public static $AdminUser;

	public function __construct(Request $request) 
	{
		parent::__Construct();

		self::$AdminUser = new AdminUser;

		$userId = Session::get('userId');

		// 获取当前控制器
		$controller = $request->controller();
		if($controller = 'Login'){
			if(empty($userId))
			{
				$this->error('请先登录...','login/login');
			}
		}
		if ($userId==1) {
           $sql='select * from Adminnode';
           $data=Db::query($sql);
       }else{
            $sql="select distinct(nodeName),rn.nodeId,u.userId,nodeController,nodeAction,nodePid from Adminuser u
                inner join userrole ur on u.userId = ur.userId
                inner join Adminrole r on r.roleId = ur.roleId
                inner join rolenode  rn on r.roleId = rn.roleId
                inner join Adminnode n on n.nodeId = rn.nodeId
                where u.userId =$userId";
            $data=Db::query($sql);
       }
       $array=$this->GetNodes($data,0);
       $this->assign('left',$array);

	}

	public function GetNodes($cate,$parent_id=0){
        // 定义空数组
        $nodes=array();
        foreach($cate as $k=>$v){
            if($v['nodePid']==$parent_id){
                $nodes[$k]=$v;
                $nodes[$k]['son']=$this->GetNodes($cate,$v['nodeId']);
            }
        }
        return $nodes;
    }
}